package com.example.statemachine.service.impl;

import com.example.statemachine.entity.Credential;
import com.example.statemachine.machine.base.FsmMachineService;
import com.example.statemachine.machine.base.FsmResBody;
import com.example.statemachine.machine.enums.EnumFsmEvent;
import com.example.statemachine.machine.enums.EnumFsmStatus;
import com.example.statemachine.service.CredentialService;
import com.example.statemachine.util.BusinessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/9 15:17
 **/
@Service
public class CredentialServiceImpl extends BaseServiceImpl<Credential> implements CredentialService {

    @Resource
    private FsmMachineService fsmSMServiceForCredential;


    @Override
    public List<Credential> all() {
        return this.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)
//    @VULock(CREDENTIAl)
    @Override
    public void tempStorage(Credential credential) {
        credential.setStatus(EnumFsmStatus.TEMP_STORAGE.ordinal());
        FsmResBody body = fsmSMServiceForCredential.execute(EnumFsmEvent.TEMP_STORAGE.ordinal(), credential.getStatus(), credential);
        if (200 != body.getCode()) {
            throw BusinessException.stateException(body.getCode(), body.getMsg());
        }
    }

    @Transactional(rollbackFor = Exception.class)
//    @VULock(CREDENTIAl)
    @Override
    public void submit(Credential credential) {
        Credential credential1 = this.selectByPrimaryKey(credential.getSid());
        if (null != credential1) {
            FsmResBody body = fsmSMServiceForCredential.execute(EnumFsmEvent.SUBMIT.ordinal(), credential.getStatus(), credential);
            if (200 != body.getCode()) {
                throw BusinessException.stateException(body.getCode(), body.getMsg());
            }
        } else {
            throw new BusinessException("数据不存在");
        }
    }

    @Transactional(rollbackFor = Exception.class)
//    @VULock(CREDENTIAl)
    @Override
    public void withDraw(Credential credential) {
        Credential optional = this.selectByPrimaryKey(credential.getSid());
        if (null != optional) {
            FsmResBody body = fsmSMServiceForCredential.execute(EnumFsmEvent.WITH_DRAW.ordinal(), credential.getStatus(), credential);
            if (200 != body.getCode()) {
                throw BusinessException.stateException(body.getCode(), body.getMsg());
            }
        } else {
            throw new BusinessException("数据不存在");
        }
    }

    @Transactional(rollbackFor = Exception.class)
//    @VULock(CREDENTIAl)
    @Override
    public void review(Credential credential) {
        Credential optional = this.selectByPrimaryKey(credential.getSid());
        if (null != optional) {
            FsmResBody body = fsmSMServiceForCredential.execute(EnumFsmEvent.REVIEW.ordinal(), credential.getStatus(), credential);
            if (200 != body.getCode()) {
                throw BusinessException.stateException(body.getCode(), body.getMsg());
            }
        } else {
            throw new BusinessException("数据不存在");
        }
    }

    @Override
    public void antiReview(Credential credential) {
        Credential optional = this.selectByPrimaryKey(credential.getSid());
        if (null != optional) {
            FsmResBody body = fsmSMServiceForCredential.execute(EnumFsmEvent.ANTI_REVIEW.ordinal(), credential.getStatus(), credential);
            if (200 != body.getCode()) {
                throw BusinessException.stateException(body.getCode(), body.getMsg());
            }
        } else {
            throw new BusinessException("数据不存在");
        }
    }


}
